﻿@using System.Web.Script.Serialization
@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@Html.Partial("_ProductVariantInfo_CreateOrEdit")


@Html.Partial("_ProductVariantAttribute_List")




@{
    var model = ViewBag.Model;
    var jsonStr = new HtmlString(new JavaScriptSerializer().Serialize(model));
}

@section scripts
{
    <script>
        var init = @jsonStr;

        function ProductVariantAttribute(id, pname, actName) {
            this.Id = ko.observable(id);
            this.ProductAttributeName = ko.observable(pname);
            this.AttributeControlTypeName = ko.observable(actName);
        }

        function AttributeControlType(controlTypeName, controlTypeValue) {

            this.ControlTypeName = ko.observable(controlTypeName);
            this.ControlTypeValue= ko.observable(controlTypeValue);
            
        }
        
        function ProductVariantModel() {
            var self = this;
            self.Id = ko.observable(init.Id);
            self.Name = ko.observable(init.Name);
            self.Price= ko.observable(init.Price);
            self.Sku = ko.observable(init.Sku);
            self.DisplayOrder= ko.observable(init.DisplayOrder);
            self.StockQuantity = ko.observable(init.StockQuantity);
            self.PictureId = ko.observable(init.PictureId);
            self.PictureUrl= ko.observable(init.PictureUrl);
            self.Published = ko.observable(init.Published);
            self.ProductId = ko.observable(init.ProductId);
            

            var attributeList = $.isEmptyObject(init.ProductVariantAttributes)? []: $.map(init.ProductVariantAttributes, function (a) {
                return new ProductVariantAttribute(a.Id, a.ProductAttributeName, a.AttributeControlTypeName);
            });

            self.ProductVariantAttributes = ko.observableArray(attributeList);
            
            var cts =  $.isEmptyObject(init.AttributeControlTypes)? []:  $.map(init.AttributeControlTypes, function (a) {
                return new AttributeControlType(a.ControlTypeName, a.ControlTypeValue);
            });

            self.AvailableAttributeControlTypes = ko.observableArray(cts);
            
        }

        var vm = new ProductVariantModel();
        ko.applyBindings(vm);
    </script>
}
